#include "expandablecard.h"
#include "ElaContentDialog.h"
#include "ElaText.h"
#include "ElaToolButton.h"
#include "ElaScrollPageArea.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGraphicsOpacityEffect>

ExpandableCard::ExpandableCard(const QString &time,
                               const QString &region,
                               const QString &content,
                               QWidget *parent)
    : QWidget(parent), isExpanded(false)
{
    setStyleSheet("border-radius: 8px; padding: 12px;");
    setMinimumHeight(75);
    // 初始化布局和内容
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 标题栏
    ElaScrollPageArea *header = new ElaScrollPageArea(this);
    QHBoxLayout *headerLayout = new QHBoxLayout(header);
    headerLayout->setContentsMargins(0, 0, 0, 0);

    regionLabel = new ElaText(region);
    regionLabel->setTextStyle( ElaTextType::TextStyle::Body);
    regionLabel->setStyleSheet("font-size: 12px;");

    timeLabel = new ElaText(time);
    timeLabel->setTextStyle(ElaTextType::TextStyle::Body);
    timeLabel->setStyleSheet("font-size: 12px;");

    _arrowButton = new ElaPushButton("↓",this);
    _arrowButton->setFixedSize(30,30);
    //_arrowButton->setElaIcon(ElaIconType::Airplay);
    _arrowButton->setCursor(Qt::PointingHandCursor);
    contentEdit = new ElaLineEdit(this);
    contentEdit->setFixedSize(250,40);
    contentEdit->setStyleSheet("color:black;");
    contentEdit->hide();
    headerLayout->addWidget(regionLabel);
    headerLayout->addWidget(contentEdit);
    headerLayout->addStretch();
    headerLayout->addWidget(timeLabel);
    headerLayout->addWidget(_arrowButton);
    headerLayout->addStretch();

    // 内容区域
    _content = new QWidget();
    _content->setStyleSheet("background-color: #3A3A3A; border-radius: 6px; padding: 8px; margin-top: 8px;");
    QVBoxLayout *contentLayout = new QVBoxLayout(_content);

    /*QLabel *signLabel = new QLabel("主要体征：心率160-180，血氧96%，呼吸频率20");
    signLabel->setStyleSheet("color: #FF5555; font-size: 12px;");
    contentLayout->addWidget(signLabel);*/

    analysisLabel = new ElaText(content);
    analysisLabel->setIsWrapAnywhere(true);
    analysisLabel->setTextStyle(ElaTextType::TextStyle::Body);
    contentLayout->addWidget(analysisLabel);

    mainLayout->addWidget(header);
    mainLayout->addWidget(_content);
    _content->setMaximumHeight(0);

    // 动画
    _animation = new QPropertyAnimation(_content, "maximumHeight");
    _animation->setDuration(300);
    _animation->setEasingCurve(QEasingCurve::InOutQuad);

    /*timeLabel->setMouseTracking(true);
    timeLabel->installEventFilter(this);*/

    connect(_arrowButton, &ElaPushButton::clicked, this, &ExpandableCard::toggle);
    connect(contentEdit,&ElaLineEdit::editingFinished,this,[=]()
    {

        /*ElaContentDialog *newDialog = new ElaContentDialog(this);
        newDialog->setLeftButtonText("确定");
        newDialog->setMiddleButtonText("取消");
        newDialog->show();*/
        analysisLabel->setText(contentEdit->text());
        timeLabel->show();
        regionLabel->show();
        contentEdit->hide();
    }
    );
    // connect(this,&ExpandableCard::signaleditEnable,this,&ExpandableCard::editEnable);
    // connect(this,&ExpandableCard::signaleditDisable,this,&ExpandableCard::editDisable);
}
void ExpandableCard::toggle() {
    isExpanded = !isExpanded;
    if(isExpanded)
       _arrowButton->setText("↑");
    else
        _arrowButton->setText("↓");
    _animation->stop();
    _animation->setStartValue(_content->maximumHeight());
    _animation->setEndValue(isExpanded ? _content->sizeHint().height() : 0);
    _animation->start();
}

void ExpandableCard::startEditing()
{
    contentEdit->setText(analysisLabel->text());
    contentEdit->show();
    regionLabel->hide();
    timeLabel->hide();
    contentEdit->setFocus();
}

void ExpandableCard::editEnable()
{
    regionLabel->setMouseTracking(true);
    regionLabel->installEventFilter(this);
}

void ExpandableCard::editDisable()
{
    regionLabel->setMouseTracking(false);
    regionLabel->removeEventFilter(this);
}
